import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
/**
 * Generated class for the Approval page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-approval',
	templateUrl: 'approval.html',
})
export class ApprovalPage {

	private statusText: any;

	private selectIndex: any;

	/**
	 * 列表
	 */
	private listItems1Page: any;
	private listItems1: Array<{
		taskId: any,
		taskName: any,
		procDefId: any,
		procInstId: any,
		procDefKey: any,
		procDefName: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	}>;
	/**
		 * 列表
		 */
	private listItems2Page: any;
	private listItems2: Array<{
		taskId: any,
		taskName: any,
		procDefId: any,
		procInstId: any,
		procDefKey: any,
		procDefName: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	}>;
	/**
		 * 列表
		 */
	private listItems3Page: any;
	private listItems3: Array<{
		taskId: any,
		taskName: any,
		procDefId: any,
		procInstId: any,
		procDefKey: any,
		procDefName: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	}>;

	/**
	 * 选择框
	 */
	private inputSearch = {
		startFileDate: '',
		endFileDate: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
		this.listItems1 = new Array();
		this.listItems2 = new Array();
		this.listItems3 = new Array();
		this.selectIndex = "0";
		this.listItems1Page = 1;
		this.listItems2Page = 1;
		this.listItems3Page = 1;
		this.statusText = {
			"0": "审核中",
			"1": "完成"
		};
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad ApprovalPage');
		this.loadData("0");
	}


	//切换
	segmentChanged(ev: any) {
		if (ev['value'] == 0) {
			this.loadData("0");
			this.selectIndex = "0";
		}else if (ev['value'] == 1) {
			this.loadData("1");
			this.selectIndex = "1";
		} else {
			this.loadData("2");
			this.selectIndex = "2";
		}
	}

	//初始化
	loadData(index: any) {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				if (index == "0") {
					this.listItems1Page = 1;
					//初始化
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//行政办公
					this.http.postJson('homeTodo', 'oa.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData("0", true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
				if (index == "1") {
					this.listItems2Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//人力资源
					this.http.postJson('homeTodo', 'hr.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData("1", true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
				if (index == "2") {
					this.listItems3Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems3Page,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//成本费用
					this.http.postJson('homeTodo', 'cost.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData("2", true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}
	//加载更多
	loadMoreData(index: any) {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				if (index == "0") {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page + 1,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//行政办公
					this.http.postJson('homeTodo', 'oa.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems1Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else if (index == "1") {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page + 1,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//人力资源
					this.http.postJson('homeTodo', 'hr.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems2Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems3Page + 1,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//成本费用
					this.http.postJson('homeTodo', 'cost.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems3Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}
	//共通列表
	loadMakeData(index: any, isnew: any, datas: any) {
		if (index == "0") {
			if (isnew)
				this.listItems1 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems1.push({
					taskId: item['taskId'],
					taskName: item['taskName'],
					procDefId: item['procDefId'],
					procDefKey: item['procDefKey'],
					procDefName: item['procDefName'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['vars']['map']['title'],
					applyUserId: item['vars']['map']['applyUserId'],
					taskCreateDate: item['taskCreateDate']
				});
			}
		} else if (index == "1") {
			if (isnew)
				this.listItems2 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems2.push({
					taskId: item['taskId'],
					taskName: item['taskName'],
					procDefId: item['procDefId'],
					procDefKey: item['procDefKey'],
					procDefName: item['procDefName'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['vars']['map']['title'],
					applyUserId: item['vars']['map']['applyUserId'],
					taskCreateDate: item['taskCreateDate']
				});
			}
		} else {
			if (isnew)
				this.listItems3 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems3.push({
					taskId: item['taskId'],
					taskName: item['taskName'],
					procDefId: item['procDefId'],
					procDefKey: item['procDefKey'],
					procDefName: item['procDefName'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['vars']['map']['title'],
					applyUserId: item['vars']['map']['applyUserId'],
					taskCreateDate: item['taskCreateDate']
				});
			}
		}
	}

	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applystarttime'] = this.inputSearch.startFileDate;
		params['applyendtime'] = this.inputSearch.endFileDate;
		data['type'] = 10; //检索类型
		data['params'] = params; //参数传递 
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				this.inputSearch.startFileDate = success['applystarttime'];
				this.inputSearch.endFileDate = success['applyendtime'];
				this.loadData(this.selectIndex);
			}
		});
	}
	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData(this.selectIndex).then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData(this.selectIndex).then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}
	//详情
	dodetail(item: any) {
		/***行政办公***/
		//请假申请
		if (item.procDefKey == "leave2") {
			this.navCtrl.push('AssistantqingjiaPage', {
				type: 6
			});
		}
		//出差申请
		if (item.procDefKey == "businessTrip") {
			this.navCtrl.push('AssistantchuchaiPage', {
				type: 7
			});
		}
		//项目立项申请
		if (item.procDefKey == "projectApprovalProcess") {
			this.navCtrl.push('AssistantlixiangPage', {
				type: 9
			});
		}
		//项目立项申请
		if (item.procDefKey == "projectPurchaseApply") {
			this.navCtrl.push('AssistantcaigouPage', {
				type: 10
			});
		}
		/***人力资源***/
		if (item.procDefKey == "hrEmployeeEntry") {
			this.navCtrl.push('ResourcesinputPage', {
				type: 6
			});
		}
	}

}